package com.example.testing.ipcdemo;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    private Handler handler;
    ArrayList<String> list = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        UserManager.sUserId = 2;
        Bundle b= new Bundle();
        Log.e(TAG, "onCreate: sUserId:" + UserManager.sUserId);
       /* try {
            serializable();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            unSerializable();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }*/
        findViewById(R.id.btn_new_process).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                        startActivity(intent);
                    }
                }).start();

            }
        });

        findViewById(R.id.btn_messenger).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MessengerActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        persistToFile();
    }

    /**
     * 序列化到本地
     */
    private void persistToFile() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                User user = new User(1, "hello world", false);
                Log.e(TAG, "persistToFile: user" + user);
                ObjectOutputStream out = null;
                try {
                    out = new ObjectOutputStream(new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/cache.text"));
                    out.writeObject(user);
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
    }

    /** 序列化 ＊*/
    public void serializable() throws IOException {
        User user = new User(0, "jake", true);
        Log.e(TAG, "serializable: user hasCode:" + user.hashCode());
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/cache.text"));
        out.writeObject(user);
        out.close();
    }
    /** 反序列化 **/
    public void unSerializable() throws IOException, ClassNotFoundException {
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(Environment.getExternalStorageDirectory().getPath() + "/cache.text"));
        User user = (User) in.readObject();
        Log.e(TAG, "serializable: newuser hasCode:" + user.hashCode());
        Log.e(TAG, "unSerializable: urid:" + user.userId + ",userName:" + user.userName + ",isMale:" + user.isMale);
        in.close();
    }
}
